<?php

include_once($ROOT . "base.inc.php");

class PageController extends BaseController {
  var $contact;
  var $address;
  var $contacts;
  var $addresses;

  function PageController() {
  	parent::__construct();
	$this->title = "Contacts";
	$this->section = "contacts";

	$action = $this->getParameter("action");
	$this->contact = new Contact();
	$this->address = new Address();

	$this->contacts = ContactLogic::findAll();
	$this->addresses = AddressLogic::findAll();

	switch (strtoupper($action))
	{
		case "SAVE" :
		  $this->parseForm();
		  if (!$this->validate()) return;

		  if ($this->getParameter("address_modified") == 1)
		  {
		  	 if (!AddressLogic::save($this->address))
		  	 {
		  	 	$this->addError("Could not save address");
		  	 	break;
		  	 }
		  	 $this->contact->address_id = $this->address->id;
		  	 $this->addresses = AddressLogic::findAll();
		  }
		  if (!ContactLogic::save($this->contact))
		  {
		  	 $this->addError("Could not save contact");
		  	 break;
		  }

		  $this->addMessage("Saved");

		  $this->contacts = ContactLogic::findAll();
		  break;
		case "LOAD" :
		  $id = $this->getParameter("id");
		  $this->contact = ContactLogic::load($id);
		  $this->address = AddressLogic::load($this->contact->address_id);
		  break;
		default :
	}
  }

  function parseForm() {
	$this->contact->readForm();
	$this->address->readForm("address_");
  }

  function validate() {
	return (sizeof($this->errs) == 0);
  }

}



?>
